package edu.mobbuzz.main;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import edu.mobbuzz.daf.bean.Rss;
import edu.mobbuzz.daf.facade.RssFacade;
import edu.mobbuzz.messaging.Messaging;
import edu.mobbuzz.rss.reader.FeedReader;


public class RssUpdater implements Job {
	private static Log _log = LogFactory.getLog(RssUpdater.class);
	private RssFacade rssFacade;
	private Messaging messaging;
	
	public RssUpdater(){
		if(rssFacade==null){
			rssFacade = new RssFacade();
		}
	}
	
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		String jobName = context.getJobDetail().getFullName();
		JobDataMap map = context.getJobDetail().getJobDataMap();
		
		if(map.containsKey("messaging")){
			messaging = (Messaging) map.get("messaging");
			
		}
		List<Rss> rssList = rssFacade.displayRssList("", "");
		FeedReader[] feed = new FeedReader[rssList.size()];
		String feedurl="";
		int i=0;
		for (Iterator iterator = rssList.iterator(); iterator.hasNext();) {
			Rss rss = (Rss) iterator.next();
			feedurl=rss.getUrl();
			feed[i]=new FeedReader(feedurl, messaging); 
			i++;
			System.out.println(feedurl + ":::: SimpleJob says: " + jobName + " executing at " + new Date());
		}
        
		
	}
	
}
